//
//  SettingInfoViewController.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/16.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "SettingInfoViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "TextFieldLeftView.h"
#import "YHMarcros.h"

@interface SettingInfoViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(nonatomic, strong)UIButton *pictureBtn;
@property(nonatomic, strong)UITextField * nameTextField;
@property(nonatomic, strong)UIButton *saveBtn;
@end

@implementation SettingInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置个人信息";
    self.view.backgroundColor = kBgColor;
    self.pictureBtn = [[UIButton alloc]init];
    [self.pictureBtn setBackgroundImage:[UIImage imageNamed:@"Avatar"] forState:UIControlStateNormal];
    [self.pictureBtn addTarget:self action:@selector(pictureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.pictureBtn];
    [self.pictureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(100);
        make.size.equalTo(CGSizeMake(87, 87));
    }];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"昵    称";
    
    TextFieldLeftView *leftView  = [[TextFieldLeftView alloc]initWithView:label];
    leftView.frame = CGRectMake(0, 0, 50, 200);
    self.nameTextField.leftView = leftView;
    self.nameTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.nameTextField];

    self.nameTextField.placeholder = @"请输入用户昵称";
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pictureBtn.bottom).offset(50);
        make.centerX.equalTo(self.view);
        make.width.equalTo(200);
        make.height.equalTo(50);

    }];
    UIView *splitView = [[UIView alloc]init];
    splitView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:splitView];
    [splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.nameTextField);
        make.top.equalTo(self.nameTextField.bottom).offset(1);
        make.height.equalTo(1);
    }];
    [self.view addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(splitView.bottom).offset(100);
        make.centerX.equalTo(self.view);
        make.leading.trailing.equalTo(self.view).offset(UIEdgeInsetsMake(0, 20, 0, 20));
        make.height.equalTo(56);
    }];
}

- (void)pictureBtnClick
{
    UIAlertController  * alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self takePhoto];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self localPhoto];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        NSString *key = nil;
        
        if (picker.allowsEditing)
        {
            key = UIImagePickerControllerEditedImage;
        }
        else
        {
            key = UIImagePickerControllerOriginalImage;
        }
        //获取图片
        UIImage *image = [info objectForKey:key];
        
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            
        }
        [self.pictureBtn setBackgroundImage:image forState:UIControlStateNormal];
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

// 开始拍照
-(void)takePhoto
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //先检查相机可用是否
        BOOL cameraIsAvailable = [self checkCamera];
        if (YES == cameraIsAvailable) {
            [self presentViewController:picker animated:YES completion:nil];
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在iPhone的“设置-隐私-相机”选项中，允许本应用程序访问你的相机。" delegate:self cancelButtonTitle:@"好，我知道了" otherButtonTitles:nil];
            [alert show];
        }
        
    }
}
- (BOOL)checkCamera
{
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(AVAuthorizationStatusRestricted == authStatus ||
       AVAuthorizationStatusDenied == authStatus)
    {
        //相机不可用
        return NO;
    }
    //相机可用
    return YES;
}
// 打开本地相册
-(void)localPhoto
{
    //本地相册不需要检查，因为UIImagePickerController会自动检查并提醒
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)save
{
   
}
#pragma mark <lazy loading>
- (UITextField *)nameTextField
{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc]init];
        [_nameTextField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    }
    return _nameTextField;
}

- (UIButton *)saveBtn
{
    if (!_saveBtn) {
        _saveBtn= [[UIButton alloc]init];
        [_saveBtn setTitle:@"保  存" forState:UIControlStateNormal];
        _saveBtn.backgroundColor =  RGBColor(239, 75, 58);
        _saveBtn.layer.cornerRadius = 5;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];

    }
    return _saveBtn;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
      [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
@end
